<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Game\Platform\Org;

class CreatePlatformOrgsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 表已创建，跳过后续操作
        if (Org::hasTable()) {
            return;
        }

        $tableName = Org::tableName();

        Org::schemaConnection()->create($tableName, function (Blueprint $table) {
            $table->id('id');
            $table->foreignId('sort')->comment('排序：值越小越靠前')->nullable()->index();
            $table->foreignId('parent_id')->comment('父ID')->default(0)->index();
            $table->string('path')->comment('依赖路径')->nullable()->index();
            $table->string('name', 64)->comment('节点名称');
            $table->string('code', 64)->comment('节点code')->unique();

            $table->unsignedInteger('deleted')->nullable()->index();
            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');
        });

        Org::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '平台--节点树'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Org::schemaConnection()->dropIfExists(Org::tableName());
    }
}
